﻿using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace Graphics3
{
    public static class Extensions
    {
        private static readonly int epsilon = 20;

        public static int Distance(this Point point1, Point point2)
        {
            return (int)Math.Round(Math.Sqrt(Math.Pow(point1.X - point2.X, 2) + Math.Pow(point1.Y - point2.Y, 2)));
        }

        public static bool IsColseTo(this Point point1, Point point2)
        {
            return point1.Distance(point2) <= epsilon;
        }

        public static void FillWithWhitePixels(this Bitmap bitmap)
        {
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite,
                bitmap.PixelFormat);

            unsafe
            {
                for (int y = 0; y < bitmapData.Height; y++)
                {
                    byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
                    for (int x = 0; x < bitmapData.Width; x++)
                    {

                        row[x * 4] = 255;// newPixel.B;
                        row[(x * 4) + 1] = 255;// newPixel.G;
                        row[(x * 4) + 2] = 255;// newPixel.R;
                    }
                }
            }

            bitmap.UnlockBits(bitmapData);
        }

        public static void FillWithWhitePixelsSet(this Bitmap bitmap)
        {
            for (int x = 0; x<bitmap.Width;++x)
            {
                for (int y = 0; y < bitmap.Height; ++y)
                {
                    bitmap.SetPixel(x, y, Color.White);
                }
            }
        }
    }
}
